package cn.com.api.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_discover_comment;
import cn.com.api.service.ApiU_discover_commentService;
import cn.com.api.service.ApiU_discover_comment_replyService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "u_discover_comment/")
public class ApiU_discover_commentController {
	
    @Autowired
    private ApiU_discover_commentService apiU_discover_commentService;
    
    @Autowired
    private ApiU_discover_comment_replyService apiU_discover_comment_replyService;

	
	private static Logger logger = Logger.getLogger(ApiU_discover_commentController.class);
	
	/**
	 * 评论增加
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "insertU_discover_comment")
	public void insertU_discover_comment(HttpServletRequest request, HttpServletResponse response) throws Exception {
		

		Map<String,Object> map = new HashMap<String,Object>();
		String title = request.getParameter("title");
		String forum_title = request.getParameter("forum_title");
		String content = request.getParameter("content");
		String type = request.getParameter("type");
		String target_id = request.getParameter("target_id");
		String user_id = request.getParameter("user_id");
		String user_name = request.getParameter("user_name");
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(target_id == null || ("").equals(target_id)){
        	map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "活动不存在");
        }else{		
			try{		
					ApiU_discover_comment u_discover_comment = new ApiU_discover_comment();				
	    		    u_discover_comment.setTitle(title);
	    		    u_discover_comment.setForum_title(forum_title);
	    		    u_discover_comment.setContent(content);
	    		    u_discover_comment.setCreate_time(sd.format(new Date()));
	    		    u_discover_comment.setType(Integer.parseInt(type));
	    		    u_discover_comment.setTarget_id(target_id);
	    		    u_discover_comment.setUser_id(user_id);
	    		    u_discover_comment.setUser_name(user_name);
	    		    u_discover_comment.setIf_effect(0);
	    		    u_discover_comment.setIs_delete(0);
	    		    u_discover_comment.setRelay_id("");
	    		    u_discover_comment.setReply_count(Long.parseLong("0"));
	    		    u_discover_comment.setGood_count(Long.parseLong("0"));
	    		    u_discover_comment.setBad_count(Long.parseLong("0"));
	    		    u_discover_comment.setClick_count(Long.parseLong("0"));
	    		    u_discover_comment.setFav_count(Long.parseLong("0"));
					
					apiU_discover_commentService.insertU_discover_comment(u_discover_comment);
					// return
	                map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "评论成功！");
			
			}catch(Exception e){
				logger.error(e);
	            e.printStackTrace();  
	    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
	    		map.put(Utils.MESSAGE, "系统异常！");
			}
        }

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	
	
     /**
	 * 根据活动ID查询评论列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_discover_comments")
	public void findU_discover_comments(HttpServletRequest request, HttpServletResponse response, PageInfo pageInfo, String user_id,
			String token) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();

		Map<String,Object> param = new HashMap<String,Object>();
		String target_id = request.getParameter("target_id");
		try{
			param.put("target_id", target_id);
			param.put("user_id", user_id);
			Map<String,Object> replay_map = new HashMap<String,Object>();
			List<Map<String,Object>> result_list = new ArrayList<Map<String,Object>>();
		    List<ApiU_discover_comment> u_discover_comments = apiU_discover_commentService.findU_discover_comments_condition(param, pageInfo);
		 	for (ApiU_discover_comment apiU_discover_comment : u_discover_comments) {
		 		result_list = new ArrayList<Map<String,Object>>();
		 		int comment_id = apiU_discover_comment.getComment_id();
		 		param.put("comment_id", comment_id);
			    List<Map<String,Object>> reply_list = apiU_discover_comment_replyService.findU_discover_comment_replysByMap((HashMap)param);
			    for (Map<String, Object> map2 : reply_list) {
			    	
			    	replay_map = new HashMap<String,Object>();
			    	replay_map.put("id", map2.get("ID"));
			    	replay_map.put("comment_id", map2.get("COMMENT_ID"));
			    	replay_map.put("user_id", map2.get("USER_ID"));
			    	replay_map.put("user_name", map2.get("USER_NAME"));
			    	replay_map.put("content", map2.get("CONTENT"));
			    	replay_map.put("create_time", map2.get("CREATE_TIME"));
			    	replay_map.put("zan_count", map2.get("ZAN_COUNT"));
			    	replay_map.put("c_avatar", map2.get("C_AVATAR"));
			    	replay_map.put("reply_user_name", map2.get("REPLY_USER_NAME"));
			    	replay_map.put("is_zan", map2.get("IS_ZAN"));
			    	result_list.add(replay_map);
				}
			    apiU_discover_comment.setReplyInfo(result_list);
			}	    
            map.put("datas", u_discover_comments);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
    
	
 
}


